package com.tencent.weibo.utils
{
	import com.abstract.IWeiboAPI;
	import com.events.WeiboEvent;
	import com.tencent.weibo.api.AbstractAPI;
	import com.tencent.weibo.lang.MessageCollection;
	import com.tencent.weibo.operation.IRequestOperation;
	
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.IOErrorEvent;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import flash.net.navigateToURL;
	
	public class TencentAPI extends AbstractAPI implements IWeiboAPI
	{
		public function TencentAPI(target:IEventDispatcher=null)
		{
			super(target);
		}
		
		private var _type:String;
		
		public function callWeiboAPI(type:String,params:Object):void
		{
			_type = type;
//			checkAccessToken();
			var url:String;
			var method:String = Tencent_URL_Method_util.url_method_util[type].method;
			var request:IRequestOperation;
			switch(type){
				case 'requestToken':
					if(null == params)params = {};
					params.oauth_callback = 'null';
					url = config.encryptURLBase+Tencent_URL_Method_util.url_method_util[type].url;
					request = consolidationParametersAndSend(url,params,null,'');
					break;
				case 'accessToken':
					url = config.encryptURLBase+Tencent_URL_Method_util.url_method_util[type].url;
					request = consolidationParametersAndSend(url,params,config.tempOauthToken,config.tempOauthSecret);
					break;
				default:
					url = config.urlBase+Tencent_URL_Method_util.url_method_util[type].url;
					request = generateDataRequest(url,params,method);
			}
		}
		
		override protected function completeHandler(event:Event):void{
			super.completeHandler(event);				
			switch(_type){
				case 'requestToken':
					requestTokenHandler(event);
					break;
				case 'accessToken':
					accessTokenHandler(event);
					break;
				default:
					var evt:WeiboEvent = new WeiboEvent(WeiboEvent.RESULT);
					evt.object = {result:data,type:_type};
					dispatchEvent(evt);
			}
			
		}
		override protected function errorHandler(event:IOErrorEvent):void{
			super.errorHandler(event);
		}
		
		/**
		 * @private
		 */		
		private function requestTokenHandler(event:Event):void
		{
			var validParms:URLVariables = new URLVariables(event.target.data);
			config.tempOauthToken = validParms.oauth_token;
			config.tempOauthSecret = validParms.oauth_token_secret;
			config.cacheToLocal("tempOauthToken",config.tempOauthToken);
			config.cacheToLocal("tempOauthSecret",config.tempOauthSecret);
			//remove listener
			(event.target as EventDispatcher).removeEventListener(Event.COMPLETE,requestTokenHandler);
			openUserAuthPage();
		}
		
		/**
		 * 打开用户授权页面
		 * @param pageTarget 窗口模式，默认是_self
		 */			
		public function openUserAuthPage(pageTarget:String="_self"):void
		{
			if(config.tempOauthToken == null)
			{
				logger.error("openUserAuthPage:{0}",MessageCollection.oauthTokenInvalid);
				return;
			}
			var url:String = config.encryptURLBase+"authorize?oauth_token="+config.tempOauthToken;
			navigateToURL(new URLRequest(url),pageTarget);
		}
		
		/**
		 * @private
		 */		
		private function accessTokenHandler(event:Event):void
		{
			var validParms:URLVariables = new URLVariables(event.target.data);
			config.oauthToken = validParms.oauth_token;
			config.oauthSecret = validParms.oauth_token_secret;
			config.cacheToLocal("oauthToken",config.oauthToken);
			config.cacheToLocal("oauthSecret",config.oauthSecret);
			//clear temp token
			config.tempOauthToken = null;
			config.tempOauthSecret = null;
			config.cacheToLocal("tempOauthToken",null);
			config.cacheToLocal("tempOauthSecret",null);
			//remove listener
			var evt:WeiboEvent = new WeiboEvent(WeiboEvent.RESULT);
			evt.object = {result:data,type:_type};
			dispatchEvent(evt);
			(event.target as EventDispatcher).removeEventListener(Event.COMPLETE,accessTokenHandler);
		}
	}
}